package com.android.vspice;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.android.vspice.swig.VSpice;

public class VisionSpice extends Activity {
	static {
		System.loadLibrary("spice");
		System.loadLibrary("vision-spice");
	}
	
	public final static int WIDTH = 640;
	public final static int HEIGHT = 480;
	CameraPreview preview;
	View viewButton;
	ImageButton btnRecog;
	boolean take = false;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        disableScreenTurnOff();
        setOrientation();
        setFullscreen();
        
        preview = new CameraPreview(this, WIDTH, HEIGHT);
        
        setContentView(preview);
        LayoutInflater inflater = getLayoutInflater();
        viewButton = inflater.inflate(R.layout.preview, null);
        addContentView(viewButton, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        
        LinearLayout linearBack = (LinearLayout)findViewById(R.id.linear_back);
        linearBack.setOnClickListener(listenFocus);
        ImageButton btnTake = (ImageButton)findViewById(R.id.button_take);
        btnTake.setOnClickListener(listenTake);
        //ImageButton btnGraph = (ImageButton)findViewById(R.id.button_graph);
        //btnGraph.setOnClickListener(listenGraph);
        btnRecog = (ImageButton)findViewById(R.id.button_recog);
        btnRecog.setOnClickListener(listenRecog);
        btnRecog.setEnabled(false);
        //TextView textStatus = (TextView)findViewById(R.id.text_status);
        //textStatus.setText(VSpice.test());
    }
    
    /**
	 * Avoid that the screen get's turned off by the system.
	 */
	public void disableScreenTurnOff() {
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	}

	/**
	 * Set's the orientation to landscape, as this is needed by AndAR.
	 */
	public void setOrientation() {
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	}

	/**
	 * Maximize the application.
	 */
	public void setFullscreen() {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}

	public void setNoTitle() {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
	}

	@Override
	protected void onPause() {
		super.onPause();
		preview.onPause();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		super.onResume();
		take = false;
		btnRecog.setEnabled(false);
		preview.onResume();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch(keyCode) {
		case KeyEvent.KEYCODE_BACK:
			new AlertDialog.Builder(this)
			.setTitle(R.string.app_name)
			.setMessage("종료하시겠습니까?")
			.setPositiveButton("예", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					finish();
				}
			})
			.setNegativeButton("아니오", null)
			.show();
			return true;
			
		case KeyEvent.KEYCODE_MENU:
			if(!take) return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		Dialog dialog = null;
		
		return dialog;
	}

	private OnClickListener listenFocus = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if(!take)
				preview.setFocus();
		}
	};
	
	private OnClickListener listenTake = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if(preview.takePicture()) {
				take = true;
				btnRecog.setEnabled(true);
				
			}else {
				take = false;
				btnRecog.setEnabled(false);
				
			}
		}
	};
	
	private OnClickListener listenRecog = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
//			
			byte[] data = preview.getData();
			byte[] rgb = VSpice.convert(data, WIDTH, HEIGHT);
			//Element[] e = VSpice.testcv(rgb, WIDTH, HEIGHT, 0);
			Intent i = new Intent(VisionSpice.this, RecogActivity.class);
			i.putExtra("image", rgb); 
			startActivity(i);
		}
	};
	
	
	private OnClickListener listenGraph = new OnClickListener() {
		@Override
		public void onClick(View v) {
			/*
			double[] data;
						
			data = new double[VSpice.GetNumData()];
			if(!VSpice.GetRealData("time", data))
				return;
			
			Intent i = new Intent(VisionSpice.this, GraphResult.class);
			*/
			
			Intent i = new Intent(VisionSpice.this, AnalysisSetting.class);
			startActivity(i);
		}
	};
}
